<?php
class UploadingsController extends AppController 
{
	public $name = 'Uploadings';
	
	public function index()
	{
		
		if($this->data)
		{
			//echo"<pre>";print_r($_FILES);exit;
			$file_uploading_dir=WWW_ROOT."img/banner/";
			/*$allowedExts = array("gif", "JPEG", "jpg", "png");
			$temp = explode(".", $_FILES["file"]["name"]);
			$extension = end($temp);
			if ((($_FILES["file"]["type"] == "image/gif")
			|| ($_FILES["file"]["type"] == "image/jpeg")
			|| ($_FILES["file"]["type"] == "image/jpg")
			|| ($_FILES["file"]["type"] == "image/pjpeg")
			|| ($_FILES["file"]["type"] == "image/x-png")
			|| ($_FILES["file"]["type"] == "image/png"))
			&& ($_FILES["file"]["size"] < 40000)
			&& in_array($extension, $allowedExts))
			  {*/
				if ($_FILES["file"]["error"] > 0)
				{
					echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
				}
				else
				{
					$ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
					$org_name=$_FILES["file"]["name"];
					$file_name=time().".".$ext;
					move_uploaded_file($_FILES["file"]["tmp_name"],
					$file_uploading_dir. $file_name);
					$this->Uploading->set('orignal_name',$org_name);
					$this->Uploading->set('name',$file_name);
					$this->Uploading->save();
					$this->Session->setFlash(__('File successfully saved'));
					$this->redirect(array('controller'=>'Uploadings','action'=>'index'));
				}
			  /*}
			else
			  {
			  echo "Invalid file";
			  }*/
			//exit;
			
		}
		$all_banners=$this->Uploading->find('all');
		//echo"<pre>";print_r($all_banners);exit;
		$this->set('all_banners',$all_banners);
	}
	public function delete($id)
	{
		$file=$this->Uploading->find('first',array('conditions'=>array('Uploading.id'=>$id)));
		if($this->Uploading->delete($id))
		{
			$file_uploading_dir=WWW_ROOT."img/banner/";
			unlink($file_uploading_dir.$file['Uploading']['name']);
		    $this->Session->setFlash(__('File successfully deleted'));
	        $this->redirect(array('controller' => 'Uploadings','action' => 'index'));
		}
	}
}
